﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace RoketSolutions.SP2010.SecurityTrimming.SecurityTrimming
{
    [Serializable]
    public class TrimmingSettings : IEnumerable<SettingDefinition>
    {
        readonly Dictionary<string, SettingDefinition> _settingDict = new Dictionary<string, SettingDefinition>();

        #region / Properties /

        public SettingDefinition this[string controlId]
        {
            get
            {
                return _settingDict.ContainsKey(controlId) ? _settingDict[controlId] : null;
            }
        }

        public bool UseTrimmingProvider { get; set; }

        #endregion

        #region / Methods /

        public bool Exists(Func<SettingDefinition, bool> filter)
        {
            return _settingDict.Values.Any(filter);
        }

        public int Count()
        {
            return _settingDict.Count;
        }

        public List<SettingDefinition> ToList()
        {
            var result = new List<SettingDefinition>(_settingDict.Values.Count);
            result.AddRange(_settingDict.Values);
            return result;
        }

        public void Add(SettingDefinition setting)
        {
            if (setting == null)
                throw new ArgumentNullException("setting");
            if (_settingDict.ContainsKey(setting.ControlId))
                throw new InvalidDataException(string.Format("item with this key '{0}' already exists", setting.ControlId));

            _settingDict.Add(setting.ControlId, setting);
        }

        #endregion

        #region / Interface Implemented Methods /

        IEnumerator<SettingDefinition> IEnumerable<SettingDefinition>.GetEnumerator()
        {
            return _settingDict.Values.GetEnumerator();
        }

        public IEnumerator GetEnumerator()
        {
            return _settingDict.Values.GetEnumerator();
        }

        #endregion

    }

    [Serializable]
    public class SettingDefinition
    {
        public SettingDefinition()
        {
            AllowAccounts = new List<AccountEntity>();
            DenyAccounts = new List<AccountEntity>();
        }

        #region / Properties /

        public string ControlId { get; set; }

        public string ControlTitle { get; set; }

        public List<AccountEntity> AllowAccounts { get; set; }

        public List<AccountEntity> DenyAccounts { get; set; }

        #endregion

    }

}